package com.yiwei.zhh.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.yiwei.zhh.R;
import com.yiwei.zhh.adapter.UsePurposeAdapter;
import com.yiwei.zhh.api.Api;
import com.yiwei.zhh.constant.Const;
import com.yiwei.zhh.util.Network;
import com.yiwei.zhh.util.PreferenceUtils;
import com.yiwei.zhh.util.RxLifeUtil;
import com.yiwei.zhh.util.ToastUtil;
import com.yiwei.zhh.util.Util;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;


/**
 * Created by xsooy-pc on 17-12-8.
 */

public class UserPurposeActivity extends BaseActivity {


    @BindView(R.id.list)
    RecyclerView list;
    //    @BindView(R.id.btn_submit)
//    TextView btnSubmit;
    @BindView(R.id.refresh)
    SwipeRefreshLayout refresh;
    private Unbinder unbinder;
    private UsePurposeAdapter adataper;
    private String selectId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setBaseContentView(R.layout.activity_user_purpose);
        baseInitView(this, "UserPurposeActivity", Const.ViewType.ACTIVITY);
        unbinder = ButterKnife.bind(this);
        init();
    }

    private void init() {
        setBackIntent();
        setTitleText("借款用途");
        setRightTextBtn("确定");
        getRightTextBtn().setVisibility(View.VISIBLE);
        getRightTextBtn().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(selectId)) {
                    ToastUtil.makeText(UserPurposeActivity.this, "请选择借款用途").show();
                    return;
                }
                PreferenceUtils.putString(UserPurposeActivity.this, "use_type", selectId);
                Intent intent = new Intent(UserPurposeActivity.this, LoanAgreementActivity.class);
                startActivity(intent);
                finish();
                overridePendingTransition(R.anim.right_enter, R.anim.left_out);
            }
        });
        adataper = new UsePurposeAdapter(this);
        adataper.setOnChooseListener(new UsePurposeAdapter.OnChooseListener() {
            @Override
            public void onClick(String content) {
                selectId = content;
            }
        });
        list.setAdapter(adataper);
        refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                getUserPurpose();
            }
        });
        getUserPurpose();
    }

    public void getUserPurpose() {
        if (!Network.checkNetwork(this)) {
            ToastUtil.makeText(this, "网络连接失败").show();
            return;
        }
        refresh.setRefreshing(true);
        Api.api_service.getDealuse().compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        refresh.setRefreshing(false);
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    adataper.update(result.data);
                });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbinder.unbind();
        unbinder = null;
    }

}
